const app = getApp();
// var apiHost = 'http://develop.jhjvip.cn:8100/hpp/';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // locationInput: '',     //添加定位初始化值
    proviceName: '',       //省
    provinceId: '',        //省id
    cityName: '',          //市
    cityId: '',            //市id
    regionName: '',        //区
    regionId: '',          //区id
    site: '请选择地区',
    provice: [],           //省列表
    city: [],              //市列表
    region: [],            //区列表
    selfName: '',           //自提点名称
    address: '',            //自提点详细地址
    phone: '',              //自提点联系方式
    id: '',                 //自提点id
    index: [0, 0, 0],      //地址默认顺序  
    none: false,            //模板框的显示/隐藏
    userId:'',
    apiHost:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    var editContent = wx.getStorageSync('editContent');//获取到修改用户的信息
    var userId = wx.getStorageSync('userId');
    var apiHost = wx.getStorageSync('apiHost')
    // var userId = '2274c839ea984aacb4f1806323a96a0d';
    that.setData({
      selfName: editContent.name,         //自提点名称
      proviceName: editContent.provinceName,  //自提点省
      provinceId: editContent.provinceId, //自提点省id
      cityName: editContent.cityName,     //自提点市
      cityId: editContent.cityId,         //自提点市id
      regionName: editContent.regionName, //自提点区
      regionId: editContent.regionId,     //自提点区id
      address: editContent.address,       //自提点详细地址
      phone: editContent.phone,           //自提点联系方式
      id: editContent.id,                 //自提点id
      userId:userId,
      apiHost: apiHost
    })

    that.provice(); //省 
  },
  //省
  provice: function () {
    var proviceName = [];
    var that = this;
    var apiHost = that.data.apiHost;
    wx.request({
      url: apiHost + 'hpp/region/province',
      header: {
        'content-type': 'application/json'
      },
      success: function (data) {
        // console.log(data.data);
        var code = data.data.code;
        
        if (code == '10000') {
          var data = data.data.data;
          that.setData({
            provice: data,
            provinceId: data[that.data.index[0]].id
          })
          that.city();//市
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
  },
  //市
  city: function () {
    var that = this;
    var provinceId = that.data.provinceId;
    var apiHost = that.data.apiHost;
    wx.request({
      url: apiHost + 'hpp/region/city',
      data: {
        provinceId: provinceId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (data) {
        // console.log(data.data.data);
        var code = data.data.code;
        if (code == '10000') {
          var data = data.data.data;
          that.setData({
            city: data,
            cityId: data[that.data.index[1]].cityId
          })
          that.region();//区
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
  },
  //区
  region: function () {
    var that = this;
    var cityId = that.data.cityId;
    var apiHost = that.data.apiHost;
    wx.request({
      url: apiHost + 'hpp/region/part',
      data: {
        cityId: cityId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (data) {
        // console.log(data);
        var code = data.data.code;
        if (code == '10000') {
        var data = data.data.data;
          that.setData({
            region: data
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  //地区选择
  bindChange: function (e) {
    const val = e.detail.value;
    this.setData({
      index: val,
    })
    this.provice();//市
  },
  //取消
  off: function () {
    this.setData({
      none: false
    })
  },
  //确认
  affirm: function () {
    var val = this.data.index;
    var provice = this.data.provice;
    var city = this.data.city;
    var region = this.data.region;
    this.setData({
      none: false,
      site: '',
      proviceName: provice[val[0]].regionName,//省名字
      provinceId: provice[val[0]].id,//省id
      cityName: city[val[1]].cityName,//市名字
      cityId: city[val[1]].cityId,//市id
      regionName: region[val[2]].partName,//区名字
      regionId: region[val[1]].partId,//区id
    })
  },
  //地区模态框
  selectsite: function () {
    this.setData({
      none: true
    })
  },
  // 修改保存自提点
  formSubmit: function (e) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    var that = this;
    var proviceName = that.data.proviceName;
    var provinceId = that.data.provinceId;
    var cityName = that.data.cityName;
    var cityId = that.data.cityId;
    var regionName = that.data.regionName;
    var regionId = that.data.regionId;
    var selfName = e.detail.value.selfName;
    var address = e.detail.value.address;
    var phone = e.detail.value.phone;
    var site = e.detail.value.site;
    var id = that.data.id;
    var userId = that.data.userId;
    var apiHost = that.data.apiHost;

    if (selfName == '') {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    } else if (site == '') {
      wx.showToast({
        title: '地区不能为空',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (address == '') {
      wx.showToast({
        title: '地址不能为空',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (phone == "") {
      wx.showToast({
        title: '手机不能为空',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (phone.length < 11) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else {
      wx.request({
        url: apiHost + 'hpp/self/lifting/edit',
        data: {
          userId: userId,
          selfName: selfName,
          proviceName: proviceName,
          provinceId: provinceId,
          cityName: cityName,
          cityId: cityId,
          regionName: regionName,
          regionId: regionId,
          address: address,
          phone: phone,
          selfId: id
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (data) {
          // console.log(data)
          var code = data.data.code;
          var msg = data.data.msg;
          if (code == '10000') {
            //跳转页面
            that.goPage('../../personal/setOneselfRaiseDot/setOneselfRaiseDot/setOneselfRaiseDot');
          } else {
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 2000,
              mask:true
            })
          }
          // 隐藏加载框  
          wx.hideLoading();
        },
        fail: function () {
          wx.showToast({
            title: '网络异常',
            icon: 'none',
            duration: 2000,
            mask:true
          })
        }
      })
    }
  },
  //删除地址
  delbtn: function () {
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    var that = this;
    var id = that.data.id;
    var userId = that.data.userId;
    var apiHost = that.data.apiHost;
    wx.request({
      url: apiHost + 'hpp/self/lifting/del',
      data: {
        userId: userId,
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (data) {
        // console.log(data);
        if (data.data.code == '10000') {
          //跳转页面
          that.goPage('../../personal/setOneselfRaiseDot/setOneselfRaiseDot');
        } else {
          wx.showToast({
            title: data.data.msg,
            icon: 'none',
            duration: 2000,
            mask:true
          })
        }
        // 隐藏加载框  
        wx.hideLoading();
      },
      fail: function () {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
  },

  /**
   * 跳转页面
   */
  goPage: function (url) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    wx.navigateBack({
      delta: 1
    })
    // wx.navigateTo({
    //   url: url,
    //   success: function () {
    //     wx.hideLoading();
    //   }
    // })
    
  }
})